package com.mark.killy.Utils;

import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectTools {

    /**
     * Read object form base64 string
     * Object save in string type instance
     * http://blog.csdn.net/dangnianmingyue_gg/article/details/47373699
     *
     * @param objStr
     * @return
     */
    public static Object readObjectFromBase64Str(String objStr) {
        Object temp = null;
        try {
            byte[] bytes = base64String2Byte(objStr);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
            ObjectInputStream objIn = new ObjectInputStream(inputStream);
            temp = objIn.readObject();
            objIn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return temp;
    }

    /**
     * Get object base64 string
     *  http://blog.csdn.net/z69183787/article/details/16883243
     *  http://longpo.iteye.com/blog/2203627
     *  http://blog.csdn.net/maoyeqiu/article/details/45748717
     *
     * @param object
     * @return
     */
    public static String getBase64ObjectString(Object object) {
        ByteArrayOutputStream aos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream objOut = new ObjectOutputStream(aos);
            objOut.writeObject(object);
            objOut.flush();
            byte[] bytes = aos.toByteArray();
            objOut.close();
            String res = byte2Base64String(bytes);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * base64字符串转byte[]
     *  http://blog.csdn.net/big1989wmf/article/details/70144803
     *  http://blog.csdn.net/chenzhou121/article/details/30756363
     * @param base64Str
     * @return
     */
    public static byte[] base64String2Byte(String base64Str){
        return Base64.decode(base64Str,Base64.DEFAULT);
    }

    /**
     * byte[]转base64
     * @param byteArr
     * @return
     */
    public static String byte2Base64String(byte[] byteArr){
        return Base64.encodeToString(byteArr,Base64.DEFAULT);
    }


    /**
     * Write object to file
     *
     * @param obj
     * @param path
     */
    public static void writeObjectToFile(Object obj, String path) {
        File file = new File(path);
        FileOutputStream out;
        try {
            out = new FileOutputStream(file);
            ObjectOutputStream objOut = new ObjectOutputStream(out);
            objOut.writeObject(obj);
            objOut.flush();
            objOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Read Object form file
     *
     * @param path
     * @return
     */
    public static Object readObjectFromFile(String path) {
        Object temp = null;
        File file = new File(path);
        FileInputStream in;
        try {
            in = new FileInputStream(file);
            ObjectInputStream objIn = new ObjectInputStream(in);
            temp = objIn.readObject();
            objIn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return temp;
    }
}
